package com.edu.exam.entity.quality.workflow;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 审核环节实体类
 * 对应数据库表：audit_step
 *
 * 根据B-02-05试题审核流程子模块设计文档实现
 * 管理审核流程的具体环节，支持人工审核和条件判断两种环节类型。
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Table(name = "audit_step")
@Comment("审核环节表")
@EntityListeners(AuditingEntityListener.class)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AuditStep {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 审核环节唯一标识符
     */
    @NotBlank(message = "审核环节ID不能为空")
    @Size(max = 50, message = "审核环节ID长度不能超过50个字符")
    @Column(name = "step_id", nullable = false, length = 50, unique = true)
    private String stepId;

    /**
     * 关联的审核流程定义ID
     */
    @NotBlank(message = "审核流程定义ID不能为空")
    @Size(max = 50, message = "审核流程定义ID长度不能超过50个字符")
    @Column(name = "flow_id", nullable = false, length = 50)
    private String flowId;

    /**
     * 审核环节代码
     */
    @NotBlank(message = "审核环节代码不能为空")
    @Size(max = 32, message = "审核环节代码长度不能超过32个字符")
    @Column(name = "step_code", nullable = false, length = 32)
    private String stepCode;

    /**
     * 审核环节名称
     */
    @NotBlank(message = "审核环节名称不能为空")
    @Size(max = 200, message = "审核环节名称长度不能超过200个字符")
    @Column(name = "step_name", nullable = false, length = 200)
    private String stepName;

    /**
     * 审核环节描述
     */
    @Column(name = "step_description", columnDefinition = "TEXT")
    private String stepDescription;

    /**
     * 审核环节类型（1-人工审核，2-条件判断）
     */
    @NotNull(message = "审核环节类型不能为空")
    @Column(name = "step_type", nullable = false)
    private Integer stepType;

    /**
     * 审核环节状态（ACTIVE-激活，INACTIVE-非激活，DEPRECATED-已废弃）
     */
    @NotBlank(message = "审核环节状态不能为空")
    @Size(max = 20, message = "审核环节状态长度不能超过20个字符")
    @Column(name = "step_status", nullable = false, length = 20)
    private String stepStatus;

    /**
     * 执行顺序
     */
    @NotNull(message = "执行顺序不能为空")
    @Column(name = "sequence", nullable = false)
    private Integer sequence;

    /**
     * 是否为必需环节
     */
    @NotNull(message = "必需环节标识不能为空")
    @Column(name = "is_required", nullable = false)
    private Boolean isRequired;

    /**
     * 是否可跳过
     */
    @NotNull(message = "可跳过标识不能为空")
    @Column(name = "is_skippable", nullable = false)
    private Boolean isSkippable;

    /**
     * 是否可并行执行
     */
    @NotNull(message = "可并行执行标识不能为空")
    @Column(name = "is_parallel", nullable = false)
    private Boolean isParallel;

    /**
     * 分配方式（MANUAL-人工指定，ROLE-角色分配，DEPARTMENT-部门分配，DYNAMIC-动态分配）
     */
    @Size(max = 20, message = "分配方式长度不能超过20个字符")
    @Column(name = "assignment_method", length = 20)
    private String assignmentMethod;

    /**
     * 分配配置（JSON格式，存储分配的详细配置）
     */
    @Column(name = "assignment_config", columnDefinition = "TEXT")
    private String assignmentConfig;

    /**
     * 审核人员要求（JSON格式，存储审核人员的要求配置）
     */
    @Column(name = "auditor_requirements", columnDefinition = "TEXT")
    private String auditorRequirements;

    /**
     * 环节条件（JSON格式，存储环节执行的触发条件）
     */
    @Column(name = "step_conditions", columnDefinition = "TEXT")
    private String stepConditions;

    /**
     * 环节动作（JSON格式，存储环节执行的动作配置）
     */
    @Column(name = "step_actions", columnDefinition = "TEXT")
    private String stepActions;

    /**
     * 环节规则（JSON格式，存储环节的执行规则）
     */
    @Column(name = "step_rules", columnDefinition = "TEXT")
    private String stepRules;

    /**
     * 超时配置（JSON格式，存储环节的超时配置）
     */
    @Column(name = "timeout_config", columnDefinition = "TEXT")
    private String timeoutConfig;

    /**
     * 通知配置（JSON格式，存储环节的通知配置）
     */
    @Column(name = "notification_config", columnDefinition = "TEXT")
    private String notificationConfig;

    /**
     * 前置环节（JSON格式，存储前置环节的ID列表）
     */
    @Column(name = "previous_steps", columnDefinition = "TEXT")
    private String previousSteps;

    /**
     * 后置环节（JSON格式，存储后置环节的ID列表）
     */
    @Column(name = "next_steps", columnDefinition = "TEXT")
    private String nextSteps;

    /**
     * 环节表单配置（JSON格式，存储环节的表单字段配置）
     */
    @Column(name = "form_config", columnDefinition = "TEXT")
    private String formConfig;

    /**
     * 预计执行时间（分钟）
     */
    @Column(name = "estimated_duration")
    private Integer estimatedDuration;

    /**
     * 环节权重
     */
    @Column(name = "step_weight")
    private Integer stepWeight;

    /**
     * 创建者ID
     */
    @Size(max = 50, message = "创建者ID长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 创建时间
     */
    @CreatedDate
    @Column(name = "create_time", nullable = false, updatable = false)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @Column(name = "update_time", nullable = false)
    private LocalDateTime updateTime;

    /**
     * 软删除标识
     */
    @NotNull(message = "软删除标识不能为空")
    @Column(name = "is_deleted", nullable = false)
    private Boolean isDeleted;

    /**
     * 删除时间
     */
    @Column(name = "delete_time")
    private LocalDateTime deleteTime;

    /**
     * 更新人
     */
    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", nullable = false, length = 50)
    private String updatedBy;

}